package com.bootdo.contract.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bootdo.contract.domain.TkzhishidianDO;
import com.bootdo.contract.domain.XuanxiangDO;
import com.bootdo.contract.domain.YutableDO;
import com.bootdo.contract.service.TkzhishidianService;
import com.bootdo.contract.service.XuanxiangService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.contract.domain.TimuDO;
import com.bootdo.contract.service.TimuService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * @author yang
 * @email 2795210596@qq.com
 * @date 2019-09-05 11:21:05
 */

@Controller
@RequestMapping("/contract/timu")
public class TimuController {
    @Autowired
    private TimuService timuService;
    @Autowired
    private TkzhishidianService tkzhishidianService;
    @Autowired
    private XuanxiangService xuanxiangService;

    @GetMapping("/timu")
    String Timu(@RequestParam("zhishidianid") Integer zhishidianid, Model model) {
        TkzhishidianDO tkzhishidianDO = tkzhishidianService.get(zhishidianid);
        model.addAttribute("tkzhishidianDO", tkzhishidianDO);
        return "contract/timu/timu";
    }

    @ResponseBody
    @GetMapping("/list")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<TimuDO> timuList = timuService.list(query);
        int total = timuService.count(query);
        PageUtils pageUtils = new PageUtils(timuList, total);
        return pageUtils;
    }

    @GetMapping("/add")
    String add(@RequestParam("zhishidianid") Integer zhishidianid, Model model) {
        Map<String, Object> map = new HashMap<>();
        map.put("zhishidianid", zhishidianid);
        List<TimuDO> list = timuService.list(map);
        model.addAttribute("xuhao", list.size());
        model.addAttribute("zhishidianid", zhishidianid);
        return "contract/timu/add";
    }

    @GetMapping("/edit/{id}")
    String edit(@PathVariable("id") Integer id, Model model) {
        TimuDO timu = timuService.get(id);
        Map<String, Object> map = new HashMap<>();
        map.put("timuid", id);
        List<XuanxiangDO> list = xuanxiangService.list(map);
        model.addAttribute("timu", timu);
        model.addAttribute("list", list);
        return "contract/timu/edit";
    }

    @GetMapping("/check")
    String check(@RequestParam("timuid") Integer timuid, Model model) {
        TimuDO timu = timuService.get(timuid);
        Map<String, Object> map = new HashMap<>();
        map.put("timuid", timuid);
        List<XuanxiangDO> list = xuanxiangService.list(map);
        for (int i = 0; i < list.size(); i++) {
            XuanxiangDO xuanxiangDO = list.get(i);
            if (i == 0) {
                xuanxiangDO.setQianzhui("A");
                list.set(i, xuanxiangDO);
            } else if (i == 1) {
                xuanxiangDO.setQianzhui("B");
                list.set(i, xuanxiangDO);
            } else if (i == 2) {
                xuanxiangDO.setQianzhui("C");
                list.set(i, xuanxiangDO);
            } else if (i == 3) {
                xuanxiangDO.setQianzhui("D");
                list.set(i, xuanxiangDO);
            } else if (i == 4) {
                xuanxiangDO.setQianzhui("E");
                list.set(i, xuanxiangDO);
            } else {
                xuanxiangDO.setQianzhui("F");
                list.set(i, xuanxiangDO);
            }
        }
        model.addAttribute("timu", timu);
        model.addAttribute("list", list);
        return "contract/timu/check";
    }

    /**
     * 保存
     */
    @ResponseBody
    @PostMapping("/save")
    public R save(TimuDO timu, @RequestParam("xuangxiang") String[] xuangxiang) {
        if (timuService.save(timu) > 0) {
            for (int i = 0; i < xuangxiang.length; i++) {
                String s = xuangxiang[i];
                XuanxiangDO xuanxiangDO = new XuanxiangDO();
                xuanxiangDO.setTimuid(timu.getId());
                xuanxiangDO.setXuanxiang(s);
                xuanxiangDO.setCreatetime(new Date());
                if (i == 0) {
                    xuanxiangDO.setQianzhui("A");
                } else if (i == 1) {
                    xuanxiangDO.setQianzhui("B");
                } else if (i == 2) {
                    xuanxiangDO.setQianzhui("C");
                } else if (i == 3) {
                    xuanxiangDO.setQianzhui("D");
                } else if (i == 4) {
                    xuanxiangDO.setQianzhui("E");
                } else {
                    xuanxiangDO.setQianzhui("F");
                }
                xuanxiangService.save(xuanxiangDO);
            }
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/update")
    public R update(TimuDO timu) {
        timuService.update(timu);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @ResponseBody
    public R remove(Integer id) {
        if (timuService.remove(id) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 删除
     */
    @PostMapping("/batchRemove")
    @ResponseBody
    public R remove(@RequestParam("ids[]") Integer[] ids) {
        timuService.batchRemove(ids);
        return R.ok();
    }

}
